<?php

namespace App\Jobs;

use App\Models\ChallengeGame;
use App\Models\LuckMoneyRule;
use App\Models\Reservation;
use App\Models\ShareLog;
use App\Models\WechatUser;
use App\Models\Withdraw;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class SendStatisticsEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $today = Carbon::today();
        $todayStr = $today->format('Y-m-d');

        //总用户
        $totUserCount = WechatUser::where('is_true', 1)->count();
        //只关注小程序
        $miniUserCount = WechatUser::where('is_true', 1)->whereNotNull('openid')->whereNull('wechat_openid')->count();
        //只关注公众号
        $officeUserCount = WechatUser::where('is_true', 1)->whereNull('openid')->whereNotNull('wechat_openid')->count();
        //同时关注小程序和公众号
        $bothUserCount = WechatUser::where('is_true', 1)->whereNotNull('openid')->whereNotNull('wechat_openid')->count();
        //当天新增
        $todayUserCount = WechatUser::where('is_true', 1)->where('created_at', '>=', $today)->count();
        //当天PK赛
        $pkCount = ChallengeGame::where('is_true', 1)->where('created_at', '>=', $today)->count();
        //当天pk分享
        $pkShareCount = ShareLog::where([['created_at', '>=', $today], ['page', 'myChallengeGameInfo']])->count();
        //当天约战
        $reservationCount = Reservation::where('created_at', '>=', $today)->count();
        //当天约战分享
        $reservationShareCount = ShareLog::where([['created_at', '>=', $today], ['page', 'reservationInfo']])->count();
        //红包发送
        $luckMoney = LuckMoneyRule::where('is_on', 1)->get();
        //提现
        $withDraw = Withdraw::where('status', 10)->get();
        Mail::send('email.statistics',
            [
                'todayStr' => $todayStr,
                'totUserCount' => $totUserCount,
                'miniUserCount' => $miniUserCount,
                'officeUserCount' => $officeUserCount,
                'bothUserCount' => $bothUserCount,
                'todayUserCount' => $todayUserCount,
                'pkCount' => $pkCount,
                'pkShareCount' => $pkShareCount,
                'reservationCount' => $reservationCount,
                'reservationShareCount' => $reservationShareCount,
                'luckMoney' => $luckMoney,
                'withDraw' => $withDraw,
            ]
            , function ($message) use ($todayStr) {
                $toArray = config('mailrec.statistics');
                foreach ($toArray as $vo) {
                    $message->to($vo);
                }
                // $message->to('1@augustor.com');

                $message->subject($todayStr . '每日统计');
            });

    }
}
